<?php
/**
 * Title:Action Class Run
 * Times:2010.04.15
 * Author:雅典娜之子
 */

define('ACCESSING', true);
include_once $_SERVER['DOCUMENT_ROOT'].'/comment/config.php';
include_once $BASE['corepath'].'base.class.php';
include_once $BASE['corepath'].'archives.class.php';
include_once $BASE['corepath'].'images.class.php';
include_once $BASE['corepath'].'page.class.php';
class Run extends Base
{
	var $admintpl;
	public function __construct()
	{
		parent::__construct();

		$action = parent::GetStr('action');
		$view = parent::GetStr('view');
		$islogin = $this->member->isLogin();
		$this->admintpl = $this->base['temppath']."admin/";
		
		if(!$islogin)
		{
			$view = 'ad_login';
		}

		if(empty($view) && empty($action))
		{
			$view = 'main';
		}

		!empty($action) && $this->action($action,$islogin);
		!empty($view) && $this->show($view,$islogin);
	}

	public function Run() {
		$this->__construct();
	}

	private function show($view,$islogin) {
		switch($view)
		{
			case 'ad_login':
				if($islogin)
				{
					parent::imperfect('您已經登入過系統，無需重複登入！!!','/admin/?view=main',3);
				}
				else 
				{
					parent::display($this->base['temppath']."admin/ad_login");
				}
				
				break;
			case 'main':
				parent::display($this->admintpl."index");
				break;
			case 'mainbody':
				parent::display($this->admintpl."main");
				break;
			case 'changepassword':
				$UserName = GetSession("UserName");
				parent::display($this->admintpl."changepass",array($UserName,$this->base['publicpath']));
				break;
			case 'Hotel_description':
				$archives = new Archives();
				$arcinfo = array();
				if($result = $archives->GetArcInfo(2))
				{
					$arcinfo[0] = $result['aid'];
					$arcinfo[1] = $result['installation'];
				}
				parent::display($this->admintpl."hotel_description",$arcinfo);
				break;
			case 'related_companies':
				$archives = new Archives();
				$arcinfo = array();
				if($result = $archives->GetArcInfo(3))
				{
					$arcinfo[0] = $result['aid'];
					$arcinfo[1] = $result['installation'];
				}
				parent::display($this->admintpl."related_companies",$arcinfo);
				break;
			case 'rates_list':
				$archives = new Archives();
				$arcinfo = array();
				if($result = $archives->GetArcInfo(4))
				{
					$arcinfo[0] = $result['aid'];
					$arcinfo[1] = $result['installation'];
				}
				parent::display($this->admintpl."rates_list",$arcinfo);
				break;
			case 'transportation_guide':
				$archives = new Archives();
				$arcinfo = array();
				if($result = $archives->GetArcInfo(5))
				{
					$arcinfo[0] = $result['aid'];
					$arcinfo[1] = $result['installation'];
				}
				parent::display($this->admintpl."transportation_guide",$arcinfo);
				break;
			case 'newsadd':
				$arcinfo = null;
				$pagetitle = "新增訊息";
				$parameter = !empty($_GET["parameter"]) ? parent::GetStr("parameter") : "formcode-fx";
				parent::display($this->admintpl."newsadd",array('parameter'=>$parameter,'pagetitle'=>$pagetitle,'arcinfo'=>$arcinfo));
				break;
			case 'newsedit':
				$aid = parent::GetStr('aid');
				empty($aid) && exit("parameter error!!");
				$arcinfo = null;
				$pagetitle = "修改訊息";
				$archives = new Archives();
				$arcinfo = $archives->GetArcInfo($aid);
				$starttime = !empty($arcinfo['starttime']) ? date("Y-m-d H:i:s",$arcinfo['starttime']) : null;
				$endtime = !empty($arcinfo['endtime']) ? date("Y-m-d H:i:s",$arcinfo['endtime']) : null;

                if(!empty($starttime))
                {
                    $startArray = split(' ', $starttime);
                    $arcinfo['starttime'] = $startArray[0];
                    $arcinfo['starttimes'] = $startArray[1];
                }

                if(!empty($endtime))
                {
                    $endArray = split(' ', $endtime);
                    $arcinfo['endtime'] = $endArray[0];
                    $arcinfo['endtimes'] = $endArray[1];
                }
                
				$parameter = $this->getformcode($arcinfo['cid']);
				parent::display($this->admintpl."newsedit",array('parameter'=>$parameter,'pagetitle'=>$pagetitle,'arcinfo'=>$arcinfo));
				break;
			case 'newslist':
				$archives = new Archives();
				$pagesize = 20;
				$pagenow = parent::PageNow();
				$start = !empty($_GET['page']) ? ($pagesize * ($pagenow - 1)) : 0;
				$limit = $start.",".$pagesize;

				if(!empty($_GET['parameter']) && $_GET['parameter'] == 'search' && !empty($_POST['namekey']))
				{
					$namekey = parent::PostStr('namekey');
					$arcdata = $archives->Search($namekey);
				}
				else
				{
					if(!empty($_GET['cid']))
					{
						$cid = parent::GetStr('cid');
						!is_numeric($cid) && exit('parameter error!!');
						$arcdata = $archives->NewsList($cid,null,$limit);
					}
					else
					{
						$arcdata = $archives->NewsList("all",null,$limit);
					}
					$parameter = !empty($_GET['cid']) ? $_GET['cid'] : null;
					if(!empty($parameter))
					{
						$parameter = $this->getformcode($parameter);
					}
				}

				$arcinfo = $arcdata[0];
				$maxnum = $arcdata[1];
				$url = !empty($_GET['cid']) ? "&cid=".parent::GetStr('cid') : "";

				$pageobj = new Page();
				$pagestr = $pageobj->getPageStr($pagesize,$pagenow,$maxnum,'?view=newslist'.$url);

				foreach($arcinfo as $key=>$value)
				{
					$cname = array();
					$cid = $value['cid'];
					$cname = $archives->GetCateName($cid);
					$arcinfo[$key]['cname'] = $cname['cname'];

                    if($value['recommend'] == 1 && $arcinfo[$key]['cname'] == '房型')
                    {
                        $arcinfo[$key]['state'] = "已推薦";
                    }
                    else
                    {
                        $arcinfo[$key]['state'] = null;
                    }
				}
				
				parent::display($this->admintpl."newslist",array($arcinfo,$parameter,$pagestr));
				break;
			case 'images':
				$archives = new Archives();
				$pagesize = 20;
				$pagenow = parent::PageNow();
				$start = !empty($_GET['page']) ? ($pagesize * ($pagenow - 1)) : 0;
				$limit = $start.",".$pagesize;

				if(!empty($_GET['aid']))
				{
					$aid = parent::GetStr('aid');
					!is_numeric($aid) && exit('parameter error!!');
					$arcdata = $archives->imageslist($aid,$limit);
				}
				else
				{
					$arcdata = $archives->imageslist("all",$limit);
				}

				$arcinfo = $arcdata[0];
				$maxnum = $arcdata[1];
				$url = !empty($_GET['aid']) ? "&aid=".parent::GetStr('aid') : "";

				$pageobj = new Page();
				$pagestr = $pageobj->getPageStr($pagesize,$pagenow,$maxnum,'?view=images'.$url);

                if(is_array($arcinfo))
                {
                    foreach($arcinfo as $key=>$value)
                    {
                        $cname = array();
                        $attribute = $value['attribute'];
                        $aname = $archives->GetArcInfo($attribute);
                        $arcinfo[$key]['aname'] = $aname['atitle'];
                    }
                }
				
				parent::display($this->admintpl."images",array($arcinfo,$pagestr));
				break;
			case 'addimages':
				$archives = new Archives();
				$arcinfo = $archives->getfxtitle();
				parent::display($this->admintpl."addimages",$arcinfo);
				break;
            case 'setting':
                $info = parent::GetSiteInfo();
                parent::display($this->admintpl."setting",$info);
				break;
            case 'writemail':
                parent::display($this->admintpl."writemail");
				break;
            case 'ajax_getuserlist':
                $user = new Member();
                $userlist = $user->getUserList();
                echo $userlist;
                break;
		}
	}

	private function action($action,$islogin)
	{
		switch($action)
		{
            case 'testsendmail':
//                include_once $this->base['corepath'].'email.class.php';
//                $info = parent::GetSiteInfo();
//                $smtpemailto = parent::GetStr('mailto');
//                $smtpserver = parent::GetStr('smtpserver');
//                $smtpserverport = parent::GetStr('smtpserverport');
//                $smtpuser = parent::GetStr('smtpuser');
//                $smtppass = parent::GetStr('smtppass');
//                $smtpusermail = parent::GetStr('smtpuser');
                $result = true;
                //$smtp = new smtp($smtpserver,$smtpserverport,true,$smtpuser,$smtppass);
                //$smtp->debug = false;
                //$result = $smtp->sendmail($smtpemailto, $smtpusermail, '這是一封測試郵件', '<h2>這是測試郵件的內容！</h2><p>當您看到這封郵件，說明您的SMTP已經配置正確！</p>', 'HTML');
                if($result == true)
                    echo "Success";
                else
                    echo "Failure";
                break;
			case 'Checklogin':
				$this->member->UserLogin();
				break;
			case 'logout':
				$this->member->ExitSession('../admin/');
				break;
			case 'changepass':
				$this->member->ResetPass();
				break;
			case 'update_arc_single':
				//$aid = trim($_POST['aid']);
				if(empty($_POST['aid']) || !is_numeric(trim($_POST['aid'])))
				{
					die("archives id error!!");
				}
				else
				{
					$aid = intval(trim($_POST['aid']));
				}

                $arcdata = parent::getPost();
				empty($_GET['key']) && exit("Post Error!!");
				$keylist = array("Hotel_description","related_companies","rates_list","transportation_guide");
				$key = trim($_GET['key']);
				!in_array($key,$keylist) && exit("Post Error!!");
				$archives = new Archives();
				$rseult = $archives->Update($aid,$arcdata);
				
				if($rseult)
				{
					parent::JumpUrl("../admin/?view={$key}");
				}
				break;
			case 'insert_news':
				$arcdata = parent::getPost();
				if(is_array($arcdata))
				{
					$arcdata['category'] = $this->getformid($arcdata['category']);
					$arcdata['arcpic'] = !empty($arcdata['arcpic']) ? $arcdata['arcpic'] : 0;
					$arcdata['price'] = !empty($arcdata['price']) ? $arcdata['price'] : "";
                    $arcdata['starttime'] = !empty($arcdata['starttime']) ? $arcdata['starttime'] : 0;
                    $arcdata['starttimes'] = !empty($arcdata['starttimes']) ? $arcdata['starttimes'] : 0;
                    $arcdata['endtime'] = !empty($arcdata['endtime']) ? $arcdata['endtime'] : 0;
                    $arcdata['endtimes'] = !empty($arcdata['endtimes']) ? $arcdata['endtimes'] : 0;
                    $arcdata['recommend'] = !empty($arcdata['recommend']) ? 1 : 0;

                    if(!empty($arcdata['starttime']))
                    {
                        $starttime = $arcdata['starttime']." ".$arcdata['starttimes'];
                        $starttime = parent::GetUnixTime($starttime);
                    }

                    if(!empty($arcdata['endtime']))
                    {
                        $endtime = $arcdata['endtime']." ".$arcdata['endtimes'];
                        $endtime = parent::GetUnixTime($endtime);
                    }

					$arcdata['starttime'] = !empty($starttime) ? $starttime : 0;
                    $arcdata['endtime'] = !empty($endtime) ? $endtime : 0;
					$arcdata['installation'] = !empty($arcdata['installation']) ? $arcdata['installation'] : 0;
					$arcdata['dateline'] = time();
					$archives = new Archives();
					$result = $archives->InsertData($arcdata);
					if(!$result)
					{
						exit('execute error!!');
					}
					else
					{
						parent::imperfect('新增成功!!','/admin/?view=newslist',2);
					}
				}
				else
				{
					exit("data error!!");
				}

				break;
			case 'update_news':
				$arcdata = parent::getPost();
				!is_array($arcdata) && exit("parameter error!!");
				$arcdata['category'] = $this->getformid($arcdata['category']);
				$arcdata['arcpic'] = !empty($arcdata['arcpic']) ? $arcdata['arcpic'] : 0;
				$arcdata['price'] = !empty($arcdata['price']) ? $arcdata['price'] : "";
                $arcdata['starttime'] = !empty($arcdata['starttime']) ? $arcdata['starttime'] : 0;
                $arcdata['starttimes'] = !empty($arcdata['starttimes']) ? $arcdata['starttimes'] : 0;
                $arcdata['endtime'] = !empty($arcdata['endtime']) ? $arcdata['endtime'] : 0;
                $arcdata['endtimes'] = !empty($arcdata['endtimes']) ? $arcdata['endtimes'] : 0;
                $arcdata['recommend'] = !empty($arcdata['recommend']) ? 1 : 0;
                
                if(!empty($arcdata['starttime']))
                {
                    $starttime = $arcdata['starttime'].$arcdata['starttimes'];
                    $starttime = parent::GetUnixTime($starttime);
                }
                
                if(!empty($arcdata['endtime']))
                {
                    $endtime = $arcdata['endtime'].$arcdata['endtimes'];
                    $endtime = parent::GetUnixTime($endtime);
                }

				$arcdata['starttime'] = !empty($starttime) ? $starttime : 0;
				$arcdata['endtime'] = !empty($endtime) ? $endtime : 0;
                
				$arcdata['installation'] = !empty($arcdata['installation']) ? $arcdata['installation'] : 0;
				$archives = new Archives();
				$result = $archives->Update($arcdata['aid'],$arcdata,'multi');
				if($result == true) parent::imperfect('修改成功!!','/admin/?view=newslist',2);
				break;
			case 'del_archives':
				$aid = parent::getPost();
				if($aid['Submit'] == '刪除所選')
				{
					$archives = new Archives();
					$result = $archives->Del($aid['id']);
					if($result == true) parent::imperfect('刪除成功!!','../admin/?view=newslist',2);
				}
				break;
            case 'addimages':
                if(!empty($_FILES) && !empty($_POST))
                {
                    $img = new images($_FILES, 'upfiles');
                    $upload = $img->upload();
                    if($upload === true)
                    {
                        $insert = $img->InsertData();
                        if($insert === true)
                        {
                            parent::imperfect('上傳成功!!','../admin/?view=images',2);
                        }
                        else
                        {
                            exit('execute error!!');
                        }
                    }
                    else
                    {
                        parent::imperfect($upload,'../admin/?view=images',2);
                    }
                }
                break;
            case 'del_img':
                $tid = $_POST['id'];
                $tids = "";
                $archives = new Archives();
                foreach($tid as $v)
                {
                    $tids .= $v.",";
                }
                $tids = substr($tids,0,-1);
                $result = $archives->del_img($tids);
                if($result === true) parent::imperfect('刪除成功!!','../admin/?view=images',2);
                break;
            case 'changesetting':
                $setting = parent::getPost();
                $result = parent::updateSetting($setting);
                true === $result && parent::imperfect('修改成功!!','../admin/?view=setting',2);
                break;
            case 'sendmail':
                include_once $this->base['corepath'].'email.class.php';
                $info = parent::GetSiteInfo();
                $data = parent::getPost();
                $smtpserver = $info[2]['svalue'];
                $smtpserverport = $info[6]['svalue'];
                $smtpuser = $info[3]['svalue'];
                $smtppass = $info[4]['svalue'];
                $smtpemailto = $data['mailto'];
                $smtpusermail = $info[5]['svalue'];
                $mailsubject = $data['subject'];
                $mailbody = $data['content'];
                $mailtype = "HTML";
                $smtp = new smtp($smtpserver,$smtpserverport,true,$smtpuser,$smtppass);
                $smtp->debug = false;
                $result = $smtp->sendmail($smtpemailto, $smtpusermail, $mailsubject, $mailbody, $mailtype);
                if(true == $result)
                    parent::imperfect('郵件已發送!!','../admin/?view=writemail',2);
                else
                    parent::imperfect('郵件發送可能遇到錯誤，請檢查您的SMTP系統的設定是否正確!!','../admin/?view=writemail',2);
                break;
		}
	}

	private function login()
	{
		parent::UserLogin();
	}

	private function getformcode($cid) 
	{
		switch($cid)
		{
			case 9:
				$parameter = 'formcode-fx';
				break;
			case 11:
				$parameter = 'formcode-yhfa';
				break;
			case 12:
				$parameter = 'formcode-zxxx';
				break;
			case 13:
				$parameter = 'formcode-zbjs';
				break;
			case 14:
				$parameter = 'formcode-ggjd';
				break;
			case 15:
				$parameter = 'formcode-msjs';
				break;
		}
		return $parameter;
	}

	private function getformid($formcode) 
	{
		switch($formcode)
		{
			case 'formcode-fx':
				$formid = 9;
				break;
			case 'formcode-yhfa':
				$formid = 11;
				break;
			case 'formcode-zxxx':
				$formid = 12;
				break;
			case 'formcode-zbjs':
				$formid = 13;
				break;
			case 'formcode-ggjd':
				$formid = 14;
				break;
			case 'formcode-msjs':
				$formid = 15;
				break;
			default:
				exit('category error!!');
				break;
		}
		return $formid;
	}
}

$run = new Run();
?>

